package com.xxr.mom.ci.charging.interworking.v2016.accepter.query;

import com.xxr.mom.ci.charging.bill.feign.ChargingFeign;
import com.xxr.mom.ci.charging.device.entity.Gun;
import com.xxr.mom.ci.charging.interworking.base.InterfaceAccepter;
import com.xxr.mom.ci.charging.interworking.dto.InterworkingConfigDto;
import com.xxr.mom.ci.charging.interworking.v2016.AbstractV2016Service;
import com.xxr.mom.ci.charging.interworking.vo.api.QueryStartChargeRequest;
import com.xxr.mom.ci.charging.interworking.vo.api.QueryStartChargeResponse;
import com.xxr.mom.ci.charging.mapper.device.GunMapper;
import com.xxr.mom.ci.common.enums.InterworkingRetEnum;
import com.xxr.mom.ci.common.exception.InterworkingException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 请求启动充电
 * 概述：此接口用于客户归属运营商请求设备归属运营商启动充电信息，充电模式为充满为止
 * 接口名称：query_start_charge
 * 接口使用方法：由基础设施运营商服务平台实现此接口，客户归属运营商服务平台方调用。
 *
 * @description: QueryStartChargeService
 * @date: 2024/8/13 22:56
 * @author: natsuki_kining
 * @version: 1.0
 */
@Service
public class QueryStartChargeAccepterService extends AbstractV2016Service<QueryStartChargeRequest, QueryStartChargeResponse> implements InterfaceAccepter<QueryStartChargeRequest, QueryStartChargeResponse> {

    @Autowired
    private GunMapper gunMapper;
    @Autowired
    private ChargingFeign chargingFeign;

    @Override
    public void validateRequestParam(QueryStartChargeRequest queryStartChargeRequest) {
        String startChargeSeq = queryStartChargeRequest.getStartChargeSeq();
        String connectorId = queryStartChargeRequest.getConnectorId();
        String qrCode = queryStartChargeRequest.getQrCode();

        if (StringUtils.isBlank(startChargeSeq) || StringUtils.isBlank(connectorId) || StringUtils.isBlank(qrCode)) {
            throw new InterworkingException(InterworkingRetEnum.BUSINESS_PARAMETER_ERROR);
        }
    }

    @Override
    public QueryStartChargeResponse dataHandler(QueryStartChargeRequest queryStartChargeRequest, InterworkingConfigDto configDto) {
        QueryStartChargeResponse response = new QueryStartChargeResponse();

        Gun gun = gunMapper.selectByConnectorId(queryStartChargeRequest.getConnectorId());
        if (Objects.isNull(gun)) {
            response.setSuccStat(1);
            response.setFailReason(1);
            return response;
        }

        // TODO 请求启动充电
        chargingFeign.queryStartCharge(null);
        return response;
    }

}
